import { getTagYear, SubjectType } from '@/apis';
import { getAreaConfig, getIndustryConfig, getTagTypeConfig } from '@/utils/filterConfig';
import { ComponentType, EDatePickerTypes } from 'vc';
import { getEnergyDepartment } from '../apis';

export function useScreen() {
  const PAGE_SIZE = 15;

  const queryData = reactive<any>({
    hbSubjectType: SubjectType.EMERGY_CONSUMPTION,
    pageNum: 1,
    pageSize: PAGE_SIZE,
    searchTxt: '',
    sortRule: '',
    provinceCode: '',
    cityCode: '',
    countryCode: '',
    industryCategoryCodeLv1: '',
    industryCategoryCodeLv2: '',
    industryCategoryCodeLv3: '',
    tagTypeCode: ''
  });

  const { data } = useRequest(getEnergyDepartment);
  const { data: tagYear } = useRequest(getTagYear, {
    defaultParams: [SubjectType.EMERGY_CONSUMPTION]
  });

  const common = ref([
    {
      ...getAreaConfig().value,
      submitFn: (val) => {
        const { lv1, lv2, lv3 } = val;
        queryData.provinceCode = lv1.join();
        queryData.cityCode = lv2.join();
        queryData.countryCode = lv3.join();
      }
    },
    {
      ...getIndustryConfig().value,
      submitFn: (val) => {
        const { lv1, lv2, lv3 } = val;
        queryData.industryCategoryCodeLv1 = lv1?.join() || '';
        queryData.industryCategoryCodeLv2 = lv2?.join() || '';
        queryData.industryCategoryCodeLv3 = lv3?.join() || '';
      }
    },
    {
      ...getTagTypeConfig(SubjectType.EMERGY_CONSUMPTION).value,
      submitFn: (data: string) => {
        queryData.tagTypeCode = data;
      }
    }
  ]);

  const screen = computed(() => {
    return [
      ...common.value,
      {
        title: '认定级别',
        component: ComponentType.MULTIPLE,
        options: data.value || [],
        submitFn: (data: string) => {
          queryData.departmentCode = data;
        }
      },
      {
        title: '认定年度',
        component: ComponentType.MULTIPLE,
        customYear: true,
        options:
          tagYear?.value?.map((i) => {
            return {
              code: i,
              name: i + '年'
            };
          }) || [],
        submitFn: (data: string) => {
          queryData.tagYear = data;
        }
      },
      {
        title: '披露日期',
        component: ComponentType.DATE,
        type: EDatePickerTypes.DATERANGE,
        shortcuts: [365, 365 * 3],
        submitFn: (data: string) => {
          queryData.noticeStartDate = data[0] || '';
          queryData.noticeEndDate = data[1] || '';
        }
      }
    ];
  });

  return {
    PAGE_SIZE,
    queryData,
    screen
  };
}
